package com.ruoyi.web.controller.Api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.model.domain.AccountInfo;
import com.ruoyi.system.model.service.IAccountInfoService;
import com.ruoyi.system.V2.inter.service.IApiReqRecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 游戏账号Controller
 *
 * @author ruoyi
 * @date 2022-05-04
 */
@RestController
@RequestMapping("api/account")
public class ApiAccountInfoController extends BaseController {
    @Autowired
    private IAccountInfoService accountInfoService;

    @Autowired
    private IApiReqRecService apiReqRecService;

    /**
     * 账号对接入库
     */
    @PostMapping("/save")
    public AjaxResult batchSave(@RequestBody List<AccountInfo> accountInfos, HttpServletRequest request) {

        AjaxResult result = new AjaxResult();
        String msg = "";
       try{
            msg = accountInfoService.batchSave(accountInfos, request);

           JSONObject resultJson = new JSONObject();
           resultJson.put("accountId",msg);

           result = AjaxResult.success(resultJson);
       }catch (Exception exception){
           exception.printStackTrace();
           result = AjaxResult.error(exception.getMessage());
       }
        apiReqRecService.saveInRec(JSON.toJSONString(accountInfos),JSON.toJSONString(result),request,"add");


        return result;
    }

    /**
     * 编辑账号
     */
    @PostMapping("/edit")
    public AjaxResult update(@RequestBody AccountInfo accountInfo, HttpServletRequest request) {
        String msg = "";
        AjaxResult result = new AjaxResult();
        try {
            accountInfoService.updateApi(accountInfo, request);
            result = AjaxResult.success();
        } catch (Exception e) {
            result =  AjaxResult.error(e.getMessage());
        }

        apiReqRecService.saveInRec(JSON.toJSONString(accountInfo),JSON.toJSONString(result),request,"edit");
        return result;
    }

    /**
     * 下架账号
     */
    @PostMapping(value = "undercarriage", name = "下架账号")
    public AjaxResult undercarriage(@RequestBody AccountInfo accountInfo, HttpServletRequest request) {

        String msg = "";
        AjaxResult result = new AjaxResult();

        try {
            accountInfoService.undercarriage(accountInfo,request);
            result = AjaxResult.success();
        } catch (Exception e) {
            result =  AjaxResult.error(e.getMessage());
        }

        apiReqRecService.saveInRec(JSON.toJSONString(accountInfo),JSON.toJSONString(result),request,"del");

        return result;
    }
    /**
     * 获取游戏大区
     */
    @GetMapping(value = "getArea/{area}", name = "获取游戏大区")
    public AjaxResult getArea(@PathVariable("area") String area) {
        try {
           List<String> areaList =  accountInfoService.getArea(area);
            return AjaxResult.success(areaList);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }
    /**
     * 获取游戏实名状态类型
     */
    @GetMapping(value = "getReal/{type}", name = "获取游戏实名状态类型")
    public AjaxResult getReal(@PathVariable("type") String type) {
        try {

           List<String> areaList =  accountInfoService.getArea(type+"段位");
            return AjaxResult.success(areaList);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }
}
